import type { TAdminInfo, TAdminMenuInfo, TAllMenuInfo, TMenuInfo, TMenus } from '@/typing/base';
import type { TreeNode, TreeSelectionKeys } from 'primevue/tree'
import { capitalize } from 'lodash'

export const arrayToTree = (items: TMenuInfo[]): TMenus[] => {
  const result = [];   // 存放结果集
  const itemMap: Record<string, any> = {};  //
  for (const item of items) {
    const id = item.id;
    const pid = item.pid;

    if (!itemMap[id]) {
      itemMap[id] = {
        children: [],
      }
    }

    itemMap[id] = {
      ...item,
      children: itemMap[id]['children']
    }

    const treeItem =  itemMap[id];

    if (pid === undefined) {
      result.push(treeItem);
    } else {
      if (!itemMap[pid]) {
        itemMap[pid] = {
          children: [],
        }
      }
      itemMap[pid].children.push(treeItem)
    }

  }
  return result;
}


export const arrayToTreeNodes = (items: TAdminMenuInfo[]): TreeNode[] => {
  const result = [];   // 存放结果集
  const itemMap: Record<string, any> = {};  //
  for (const item of items) {
    const id = item.id;
    const pid = item.pid;

    if (!itemMap[id]) {
      itemMap[id] = {
        children: [],
      }
    }

    itemMap[id] = {
      // ...item,
      key: item.id,
      label: item.name,
      data: item.id,
      isUsed: item.isUsed,
      children: itemMap[id]['children']
    }

    const treeItem =  itemMap[id];

    if (pid === undefined) {
      result.push(treeItem);
    } else {
      if (!itemMap[pid]) {
        itemMap[pid] = {
          children: [],
        }
      }
      itemMap[pid].children.push(treeItem)
    }

  }
  return result;
}

export const expendAll = (nodes: TreeNode[]) => {
  const expandedKeys: any = {};
  const expandNode = (node: any) => {
    if (node.children && node.children.length) {
      expandedKeys[node.key] = true;
      for (const child of node.children) {
        expandNode(child);
      }
    }
  };

  for (const node of nodes) {
    expandNode(node);
  }
  return expandedKeys
}

export function treeSelecedKeys(tree: TreeNode[]) {
  // let result = []
  const selectedKeys: Record<string | number, TreeSelectionKeys> = {}
  const transform = (tree: TreeNode) => {
   tree.forEach((item: TreeNode) => {
    const children = item?.children || []
    // delete item.children
    if (children.length) {
     const checked = children.filter(item => item.isUsed === 1)
     const isChecked = checked.length === children.length
     // 如果 子节点 有选中的，才添加
     if (checked.length) {
      // result.push({
      //  ...item,
      //  checked: isChecked,
      //  partialChecked: !isChecked
      // })
      selectedKeys[item.key as string] = {
       checked: isChecked,
       partialChecked: !isChecked
      }
     }
     transform(children)
    } else {
     if (item.isUsed === 1) {
      // result.push({
      //  ...item,
      //  checked: item.isUsed === 1,
      //  partialChecked: false
      // })
      selectedKeys[item.key as string] = {
       checked: item.isUsed === 1,
       partialChecked: false
      }
     }
    }
   })
  }
  transform(tree)
  // console.log('result', result)
  return selectedKeys
 }


 export const arrayToTreeTableNodes = (items: TAllMenuInfo[]): TreeNode[] => {
  const result = [];   // 存放结果集
  const itemMap: Record<string, any> = {};  //
  for (const item of items) {
    const id = item.id;
    const pid = item.pid;

    if (!itemMap[id]) {
      itemMap[id] = {
        children: [],
      }
    }

    itemMap[id] = {
      key: item.id + '',
      data: {
        name: item.name,
        sort: item.sort,
        path: item.path,
        icon: item.icon,
        isUsed: item.isUsed === 1
      },
      children: itemMap[id]['children']
    }

    const treeItem =  itemMap[id];

    if (pid === undefined) {
      result.push(treeItem);
    } else {
      if (!itemMap[pid]) {
        itemMap[pid] = {
          children: [],
        }
      }
      itemMap[pid].children.push(treeItem)
    }

  }
  return result;
}


// 换行输出
export const replaceTextarea = (str: string) => {
  if (!str) return str
  var reg = new RegExp("\r\n", "g");
  var reg1 = new RegExp(" ", "g");
  str = str.replace(reg, "＜br＞");
  str = str.replace(reg1, "＜p＞");
  return str;
}
